﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebMassage.Models
{
    /// <summary>
    /// Lop bussiness logic lay thong tin ve PageInfo
    /// </summary>
    public class PageInfoResponsitory
    {
        private MassageDBDataContext db = null;

        public PageInfoResponsitory()
        {
            db = new MassageDBDataContext();
        }

        public IQueryable<PageInfo> All()
        {
            return db.PageInfos;
        }

        public PageInfo GetById(int id)
        {
            return db.PageInfos.SingleOrDefault(p => p.PageID == id);
        }

        public IQueryable<PageInfo> GetByGroup(string group)
        {
            return db.PageInfos.Where(p => p.Group == group).OrderBy(p => p.Order);
        }
                
        public bool Add(PageInfo pageInfo)
        {
            try
            {
                db.PageInfos.InsertOnSubmit(pageInfo);
                Save();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool Delete(PageInfo pageInfo)
        {
            try
            {
                db.PageInfos.DeleteOnSubmit(pageInfo);
                Save();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public void Save()
        {
            db.SubmitChanges();
        }

        public void Dispose()
        {
            if (db != null)
            {
                db.DatabaseExists();
                db.Dispose();
            }
        }
    }

    /// <summary>
    /// Lop bussiness logic lay thong tin ve New
    /// </summary>
    public class NewResponsitory
    {
        private MassageDBDataContext db = null;

        public NewResponsitory()
        {
            db = new MassageDBDataContext();
        }

        public IQueryable<New> All()
        {
            return db.News;
        }

        public New GetById(string id)
        {
            return db.News.SingleOrDefault(n => n.NewID.ToString() == id);
        }

        public IQueryable<New> GetNewShowInHome(int num)
        {
            return db.News.OrderBy(n => n.CreatedDate).Take(num);
        }

        public IQueryable<New> Take(int num)
        {
            return db.News.OrderBy(n => n.CreatedDate).Take(num);
        }
        
        public bool Add(New entity)
        {
            try
            {
                db.News.InsertOnSubmit(entity);
                Save();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool Delete(New entity)
        {
            try
            {
                db.News.DeleteOnSubmit(entity);
                Save();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public void Save()
        {
            db.SubmitChanges();
        }

        public void Dispose()
        {
            if (db != null)
            {
                db.DatabaseExists();
                db.Dispose();
            }
        }
    }

    /// <summary>
    /// Lop bussiness logic lay thong tin ve KTVInfo
    /// </summary>
    public class KTVInfoResponsitory
    {
        private MassageDBDataContext db = null;

        public KTVInfoResponsitory()
        {
            db = new MassageDBDataContext();
        }

        public IQueryable<KTVInfo> All()
        {
            return db.KTVInfos.OrderBy(ktv => ktv.KTVNum);
        }

        public KTVInfo GetById(string id)
        {
            return db.KTVInfos.SingleOrDefault(k => k.KTVId.ToString() == id);
        }

        public IQueryable<KTVInfo> GetByName(string name)
        {
            return db.KTVInfos.Where(k => k.Name.Contains(name) || name.Contains(k.Name));           
        }

        public IQueryable<KTVInfo> GetByKTVNum(string KTVNum)
        {
            return db.KTVInfos.Where(k => k.KTVNum == KTVNum).OrderBy(k => k.KTVNum);
        }

        public IQueryable<KTVInfo> GetByWeight(int weight)
        {
            return db.KTVInfos.Where(k => k.Weight == weight).OrderBy(k => k.KTVNum);
        }

        public IQueryable<KTVInfo> GetByHeight(int height)
        {
            return db.KTVInfos.Where(k => k.Height == height).OrderBy(k => k.KTVNum);
        }

        public IQueryable<KTVInfo> GetByRandom(int num)
        {
            return db.KTVInfos.Take(num).OrderBy(k => k.KTVNum);
        }
        
        public bool Add(KTVInfo ktvInfo)
        {
            try
            {
                ktvInfo.KTVId = Guid.NewGuid();
                db.KTVInfos.InsertOnSubmit(ktvInfo);
                Save();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool Delete(KTVInfo ktvInfo)
        {
            try
            {
                db.KTVInfos.DeleteOnSubmit(ktvInfo);
                Save();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public void Save()
        {
            db.SubmitChanges();
        }

        public void Dispose()
        {
            if (db != null)
            {
                db.DatabaseExists();
                db.Dispose();
            }
        }
    }
    
    /// <summary>
    /// Lop bussiness logic lay thong tin ve KTVPic
    /// </summary>
    public class KTVPicResponsitory
    {
        private MassageDBDataContext db = null;

        public KTVPicResponsitory()
        {
            db = new MassageDBDataContext();
        }

        public IQueryable<KTVPic> All()
        {
            return db.KTVPics;
        }

        public KTVPic GetById(int id)
        {
            return db.KTVPics.SingleOrDefault(pic => pic.PID == id);
        }

        public IQueryable<KTVPic> GetShowInHome()
        {
            var ktvShows = db.KTVInfos.Where(ktv => ktv.ShowInHome == true);
            

            return db.KTVPics.Where(pic => pic.KTVInfo.ShowInHome == true).OrderBy(pic => pic.PID);
        }

        public IQueryable<KTVPic> GetByKtvNum(string num)
        {
            return db.KTVPics.Where(pic => pic.KTVInfo.KTVNum == num).OrderBy(pic => pic.PID);
        }

        public bool Add(KTVPic ktvPic)
        {
            try
            {
                db.KTVPics.InsertOnSubmit(ktvPic);
                Save();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool Delete(KTVPic ktvPic)
        {
            try
            {
                db.KTVPics.DeleteOnSubmit(ktvPic);
                Save();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool Delete(string ktvNum)
        {
            try
            {
                var ktvPics = GetByKtvNum(ktvNum);
                foreach (KTVPic item in ktvPics)
                {
                    db.KTVPics.DeleteOnSubmit(item);
                }
                Save();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public void Save()
        {
            db.SubmitChanges();
        }

        public void Dispose()
        {
            if (db != null)
            {
                db.DatabaseExists();
                db.Dispose();
            }
        }
    }
}